<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房间编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房间编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区编号" prop="villageId">
        <el-input
          v-model="queryParams.villageId"
          placeholder="请输入小区编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区名称" prop="villageName">
        <el-input
          v-model="queryParams.villageName"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单元号" prop="unitNum">
        <el-input
          v-model="queryParams.unitNum"
          placeholder="请输入单元号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="栋号" prop="buildingNum">
        <el-input
          v-model="queryParams.buildingNum"
          placeholder="请输入栋号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层号" prop="floorNum">
        <el-input
          v-model="queryParams.floorNum"
          placeholder="请输入楼层号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房间号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:house:add']"
        >新增</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:house:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:house:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:house:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="houseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间编号" width="300" align="center" prop="houseId" />
<!--      <el-table-column label="小区编号" align="center" prop="villageId" />-->
      <el-table-column label="小区名称" align="center" prop="villageName" />
      <el-table-column label="单元号" align="center" prop="unitNum" />
      <el-table-column label="栋号" align="center" prop="buildingNum" />
      <el-table-column label="楼层号" align="center" prop="floorNum" />
      <el-table-column label="房间号" align="center" prop="roomNum" />
      <el-table-column label="操作"  width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:house:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:house:remove']"
          >删除
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-user"
              @click="handleHolderAdd(scope.row)"
              v-hasPermi="['system:householder:add']"
          >添加住户
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

<!--        <el-form-item label="小区编号" prop="villageId">-->
<!--          <el-input v-model="form.villageId" placeholder="请输入小区编号" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="小区名称" prop="villageName">-->
<!--          <el-input v-model="form.villageName" placeholder="请输入小区名称" />-->
<!--        </el-form-item>-->

        <el-form-item label="选择小区"  v-model="form.villageName">
          <!--            v-model="form.villageName"-->
          <el-select
            @change="handlerSelectChanged"
            v-model="value"
            filterable
            remote
            reserve-keyword
            placeholder="请输入小区"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
<!--          <el-input  placeholder="请输入小区名称" />-->
        </el-form-item>

        <el-form-item label="单元号" prop="unitNum">
          <el-input v-model="form.unitNum" placeholder="请输入单元号" />
        </el-form-item>
        <el-form-item label="栋号" prop="buildingNum">
          <el-input v-model="form.buildingNum" placeholder="请输入栋号" />
        </el-form-item>
        <el-form-item label="楼层号" prop="floorNum">
          <el-input v-model="form.floorNum" placeholder="请输入楼层号" />
        </el-form-item>
        <el-form-item label="房间号" prop="roomNum">
          <el-input v-model="form.roomNum" placeholder="请输入房间号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改住户对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="80px">
<!--        <el-form-item label="住户编号" prop="holderId">-->
<!--          <el-input v-model="form.holderId" placeholder="请输入住户编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="房子编号" prop="houseId">
          <el-input readonly v-model="form2.houseId" placeholder="请输入房子编号" />
        </el-form-item>
        <el-form-item label="住户姓名" prop="holderName">
          <el-input v-model="form2.holderName" placeholder="请输入住户姓名" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form2.phone" placeholder="请输入住户手机号码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse, getHouse, delHouse, addHouse, updateHouse, exportHouse } from "@/api/system/house";
import {listVillage} from "@/api/system/village";
import {addHouseholder, updateHouseholder} from "@/api/system/householder";

export default {
  name: "House",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示添加住户的弹出层
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        villageId: null,
        villageName: null,
        unitNum: null,
        buildingNum: null,
        floorNum: null,
        roomNum: null
      },
      // 表单参数
      form: {},
      //住户表单
      form2:{},
      //住户标题
      title2:'',
      // 表单校验
      rules: {},
      options: [],
      value: [],
      list: [],
      states: []
    };
  },
  created() {
    this.getList();
    this.getVillageList()


  },
  methods:{

    //添加住户
    handleHolderAdd(row) {
      this.reset();
      this.open2 = true;
      this.title2 = "添加住户";
      this.form2.houseId=row.houseId;
    },
    handlerSelectChanged(value){
      this.form.villageId=value;
      let that=this;
      //遍历查到小区名称，入库
      this.villageList.find(item=>{
        if(item.villageId===value){
          that.form.villageName=item.villageName;
        }
      })
      console.log(this.form.villageId);
      console.log(this.form.villageName);
    },
    /** 查询住宅小区列表 */
    getVillageList() {
      this.loading = true;
      listVillage(this.queryParams).then(response => {
        this.villageList = response.rows;
        this.loading = false;
        //console.log(response);
        this.list = response.rows.map(item => {
          //return {'value':`${item.villageName}`, 'label': `${item.villageId}`};
          return {'label':`${item.villageName}`, 'value': `${item.villageId}`};
        });
      });
    },

    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter(item => {
            return item.label.toLowerCase()
              .indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options = [];
      }
    },

    /** 查询房间列表 */
    getList() {
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        houseId: null,
        villageId: null,
        villageName: null,
        unitNum: null,
        buildingNum: null,
        floorNum: null,
        roomNum: null
      };
      this.resetForm("form");
    },
    // 表单重置2
    reset2() {
      this.form2 = {
        houseId: null,
        holderName:null,
        phone:null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const houseId = row.houseId || this.ids
      getHouse(houseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.houseId != null) {
            updateHouse(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交住户信息按钮 */
    submitForm2() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.holderId != null) {
            updateHouseholder(this.form2).then(response => {
              this.msgSuccess("修改成功");
              this.open2 = false;
              //this.getList();
            });
          } else {
            addHouseholder(this.form2).then(response => {
              this.msgSuccess("新增成功");
              this.open2 = false;
              //this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const houseIds = row.houseId || this.ids;
      this.$confirm('是否确认删除房间编号为"' + houseIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delHouse(houseIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有房间数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportHouse(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
}
</script>
